package com.item.mall.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.item.mall.entity.Product;
import com.item.mall.entity.User;
import com.item.mall.service.CartService;
import com.item.mall.service.ProductCategoryService;
import com.item.mall.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 张津瑞、彭家博
 * @since 2021-07-31
 */
@Controller
@RequestMapping("/product")
public class ProductController {
    @Autowired
    ProductService productService;
    @Autowired
    ProductCategoryService productCategoryService;
    @Autowired
    CartService cartService;

    @GetMapping("/list/{type}/{id}")
    public ModelAndView getList(@PathVariable("type") String type,
                                @PathVariable("id") Integer id,
                                HttpSession session){
        ModelAndView modelAndView =new ModelAndView();
        modelAndView.setViewName("productList");
        modelAndView.addObject("productList",productService.findProduct(type,id));
        modelAndView.addObject("list",productCategoryService.getAllProductCategoryVO());
        User user =(User) session.getAttribute("user");
        if(user==null) modelAndView.addObject("cartList",new ArrayList<>());
        else modelAndView.addObject("cartList",cartService.getCartList(user.getId()));
        return modelAndView;
    }

    @GetMapping("/detail/{id}")
    public ModelAndView getProduct(@PathVariable("id") Integer productId,
                                   HttpSession session){
        ModelAndView modelAndView =new ModelAndView();
        modelAndView.setViewName("productDetail");
        modelAndView.addObject("list",productCategoryService.getAllProductCategoryVO());
        modelAndView.addObject("product",productService.getById(productId));
        User user =(User) session.getAttribute("user");
        if(user==null) modelAndView.addObject("cartList",new ArrayList<>());
        else modelAndView.addObject("cartList",cartService.getCartList(user.getId()));
        return modelAndView;
    }


    @PostMapping("/findByKey")
    public ModelAndView findByKey(String keyWord,HttpSession session){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("productList");
        //根据关键字查询
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.like("name",keyWord);
        modelAndView.addObject("productList",productService.list(wrapper));
        modelAndView.addObject("list",productCategoryService.getAllProductCategoryVO());
        User user =(User) session.getAttribute("user");
        if(user==null) modelAndView.addObject("cartList",new ArrayList<>());
        else modelAndView.addObject("cartList",cartService.getCartList(user.getId()));
        return modelAndView;
    }


}

